layui.define("layer", function(exports){
    //鎻愮ず锛氭ā鍧椾篃鍙互渚濊禆鍏跺畠妯″潡锛屽锛歭ayui.define('layer', callback);
    var $ = layui.jquery,
        form = layui.form;
    // 杩斿洖鐨勫璞?
    var returnObj = {
        /**
         * 鐪佸競鍘夸笁绾ц仈鍔?
         *
         * @param { string } [form] [form鏄痩ayui.form瀵硅薄]
         * @param {string} [province] [鐪佸鍣ㄧ殑name鍚嶅瓧 ]
         *    eg:<select name="province">
         * @param {string} [city] [鐪佸鍣ㄧ殑name鍚嶅瓧]
         * @param {string} [district] [鍘垮鍣ㄧ殑name鍚嶅瓧]
         * @return {undefined} [鏃犺繑鍥炲€糫
         */
        showCity: function(province, city, district) {
            //AreaData鏄湴鍧€浼犺繃鏉ョ殑json瀵硅薄
            var AreaData = $.rawCitiesData;
            if("undefined" == typeof AreaData) {
                layer.open({
                    title: '绯荤粺鎻愮ず'
                    ,content: '璋冪敤showCity涔嬪墠璇峰紩鍏ュ湴鍧€鏁版嵁'
                });
                return;
            }
            var htmlProvince = '',
                htmlCity = '',
                htmlDistrict = '',
                cityData = '',
                districtData = '',
                $province = $('select[name='+province+']'),
                $city = $('select[name='+city+']'),
                $district = $('select[name='+district+']'),
                provVal = $province.attr('data-area') || '', // 鐪侀粯璁ゅ€?
                cityVal = $city.attr('data-area') || '', // 甯傞粯璁ゅ€?
                distVal = $district.attr('data-area') || ''; // 鍘块粯璁ゅ€?
            $province.find('option').not(':first').remove();
            $city.find('option').not(':first').remove();
            $district.find('option').not(':first').remove();
            form.render('select');

            // 鍔犺浇鐪佹暟鎹?
            loadProvince();
            function loadProvince() {
                AreaData.forEach(function(v, i) {
                    htmlProvince += '<option value='+v.code+'>'+v.name+'</option>';
                });
                $province.append(htmlProvince);

                // 榛樿閫変腑鐪?
                defaultSelect($province, provVal, function(code) {
                    cityData = findPlace(AreaData, code);
                    loadCity(cityData, true);
                });

                form.render('select');

            }

            // 鍔犺浇鍩庡競鏁版嵁
            function loadCity(city, hasDefault) {
                $city.find('option').not(':first').remove();
                htmlCity = '';
                var city = city || [];
                city.forEach(function(v, i) {
                    htmlCity += '<option value='+v.code+'>'+v.name+'</option>';
                });
                $city.append(htmlCity);

                // 榛樿鍔犺浇鎵嶆墽琛岋紝鎵嬪姩閫変腑涓嶆墽琛?
                if (hasDefault) {
                    // 榛樿閫変腑鍩庡競
                    defaultSelect($city, cityVal, function(code) {
                        // console.log(code)
                        // 鍔犺浇榛樿鍩庡競鏁版嵁
                        districtData = findPlace(cityData, code);
                        loadDistrict(districtData, true);
                    });
                }

                form.render('select');
            }

            // 鍔犺浇鍘挎暟鎹?
            function loadDistrict(districtData, hasDefault) {
                $district.find('option').not(':first').remove();
                htmlDistrict = '';
                var districtData = districtData || [];
                districtData.forEach(function(v, i) {
                    htmlDistrict += '<option value='+v.code+'>'+v.name+'</option>';
                });
                $district.append(htmlDistrict);

                if (hasDefault) {
                    // 榛樿閫変腑鍘?
                    defaultSelect($district, distVal);
                }

                form.render('select');
            }

            // 鏌ユ壘鍦扮偣鏁版嵁
            function findPlace(data, code) {
                var resData = [];
                data.forEach(function(v, i) {
                    if (v.code === code) {
                        resData = v.sub
                    }
                });

                return resData;
            }

            /*
            * 鏌ユ壘榛樿閫変腑椤?
            * @param {object} jq鍏冪礌
            * @param {string} 榛樿鍊?
            * @param {function} 鍥炶皟鍑芥暟
            */
            function defaultSelect($el, SelectVal, callback) {
                $('option', $el).each(function(i, el) {
                    var $this = $(this);
                    var optVal = $this.html();
                    var code;
                    if (optVal == SelectVal) {
                        $this.attr("selected", true);
                        code = $this.val();
                        // 鍔犺浇榛樿鍩庡競鏁版嵁
                        callback && callback(code);
                    }
                });
            }

            // 鐪侀€夋嫨
            form.on('select('+province+')', function(data) {
                var code = data.value;
                // console.log(data.elem); //寰楀埌select鍘熷DOM瀵硅薄
                // console.log(code); //寰楀埌琚€変腑鐨勫€?
                if (code != '') {

                    cityData = findPlace(AreaData, code);

                    loadCity(cityData);
                    loadDistrict();

                } else {
                    loadCity();
                    loadDistrict();
                }
                // console.log(data.othis); //寰楀埌缇庡寲鍚庣殑DOM瀵硅薄
            });
            // 甯傞€夋嫨
            form.on('select('+city+')', function(data){
                var code = data.value;
                if (code != '') {

                    districtData = findPlace(cityData, code);

                    loadDistrict(districtData);
                    // console.log(data.elem); //寰楀埌select鍘熷DOM瀵硅薄
                } else {
                    loadDistrict();
                }
                // console.log(data.othis); //寰楀埌缇庡寲鍚庣殑DOM瀵硅薄
            });
            form.on('select('+district+')', function(data){
                // console.log(data.elem); //寰楀埌select鍘熷DOM瀵硅薄
                // console.log(data.value); //寰楀埌琚€変腑鐨勫€?
                // console.log(data.othis); //寰楀埌缇庡寲鍚庣殑DOM瀵硅薄
            });
        },
        /**
         * 鑾峰彇鐪佸競鍘挎暟鎹?
         *
         * @param { object } [address] [address eg:骞夸笢鐪佸箍宸炲競澶╂渤鍖篯
         * @return {object} [address][鏍规嵁code鐮佽繑鍥炲湴鍧€鍚嶇О]
         */
        getCity: function(address) {
            //AreaData鏄湴鍧€浼犺繃鏉ョ殑json瀵硅薄
            var AreaData = $.rawCitiesData;
            if("undefined" == typeof AreaData) {
                layer.open({
                    title: '绯荤粺鎻愮ず'
                    ,content: '璋冪敤getCity涔嬪墠璇峰紩鍏ュ湴鍧€鏁版嵁'
                });
                return;
            }
            var province = address.province,
                city = address.city,
                district = address.district,
                provinceName = '',
                cityName = '',
                districtName = '';

            function findIndex(arr, target) {
                return arr.findIndex(function(v, i) {
                    return target == v.code;
                })
            }

            if (province) {
                var findProvinceIndex = findIndex(AreaData, province);
                provinceName = AreaData[findProvinceIndex].name;
            }

            if (province && city) {
                var findCityIndex = findIndex(AreaData[findProvinceIndex].sub, city);
                cityName = AreaData[findProvinceIndex].sub[findCityIndex].name;
            }

            if (province && city && district) {
                var findDistrictIndex = findIndex(AreaData[findProvinceIndex].sub[findCityIndex].sub, district);

                districtName = AreaData[findProvinceIndex].sub[findCityIndex].sub[findDistrictIndex].name;
            }

            return {
                provinceName: provinceName,
                cityName: cityName,
                districtName: districtName
            }
        }
    }

    // exports module
    exports('common', returnObj);
});